﻿// Decompiled with JetBrains decompiler
// Type: 我的小工具.SQLHelper
// Assembly: 开发辅助工具, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null
// MVID: F78E5980-5E94-4E46-ABDF-DC8C6CBF52AA
// Assembly location: K:\Tools\工具\开发辅助工具庆六一版\开发辅助工具2015-06-01\开发辅助工具.exe

using System;
using System.Data;
using System.Data.SqlClient;

namespace 我的小工具
{
  internal class SQLHelper
  {
    public static bool dbCanUse = true;
    private SqlConnection con;
    private static string constr;

    public static string ConnectString
    {
      get
      {
        return SQLHelper.constr;
      }
      set
      {
        if (value != SQLHelper.constr)
          SQLHelper.dbCanUse = true;
        SQLHelper.constr = value;
      }
    }

    public static void SetConnSting(string strServerName, string strDBName, string strUserName, string strPwd)
    {
      SQLHelper.ConnectString = string.Format("server={0};database={1};uid={2};pwd={3}", (object) strServerName, (object) strDBName, (object) strUserName, (object) strPwd);
    }

    public static int ExecuteSql(string Sqlstr)
    {
      using (SqlConnection sqlConnection = new SqlConnection(SQLHelper.ConnectString))
      {
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = Sqlstr;
        sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlConnection.Close();
        return 1;
      }
    }

    public static string GetDataItemString(string Sqlstr)
    {
      using (SqlConnection sqlConnection = new SqlConnection(SQLHelper.ConnectString))
      {
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = Sqlstr;
        sqlConnection.Open();
        string @string = Convert.ToString(sqlCommand.ExecuteScalar());
        sqlConnection.Close();
        return @string;
      }
    }

    public static int ExecuteSql(string Sqlstr, SqlParameter[] param)
    {
      using (SqlConnection sqlConnection = new SqlConnection(SQLHelper.ConnectString))
      {
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = Sqlstr;
        sqlCommand.Parameters.AddRange(param);
        sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlConnection.Close();
        return 1;
      }
    }

    public static SqlDataReader ExecuteReader(string Sqlstr)
    {
      SqlConnection sqlConnection = new SqlConnection(SQLHelper.ConnectString);
      try
      {
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = Sqlstr;
        sqlConnection.Open();
        return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
      }
      catch
      {
        return (SqlDataReader) null;
      }
    }

    public static DataTable ExecuteDt(string Sqlstr)
    {
      using (SqlConnection selectConnection = new SqlConnection(SQLHelper.ConnectString))
      {
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(Sqlstr, selectConnection);
        DataTable dataTable = new DataTable();
        selectConnection.Open();
        sqlDataAdapter.Fill(dataTable);
        selectConnection.Close();
        return dataTable;
      }
    }

    public static DataSet ExecuteDs(string Sqlstr)
    {
      using (SqlConnection selectConnection = new SqlConnection(SQLHelper.ConnectString))
      {
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(Sqlstr, selectConnection);
        DataSet dataSet = new DataSet();
        selectConnection.Open();
        sqlDataAdapter.Fill(dataSet);
        selectConnection.Close();
        return dataSet;
      }
    }

    public int RunProc(string procName)
    {
      SqlCommand command = this.CreateCommand(procName, (SqlParameter[]) null);
      command.ExecuteNonQuery();
      this.Close();
      return (int) command.Parameters["ReturnValue"].Value;
    }

    public int RunProc(string procName, SqlParameter[] prams)
    {
      SqlCommand command = this.CreateCommand(procName, prams);
      command.ExecuteNonQuery();
      this.Close();
      return (int) command.Parameters[0].Value;
    }

    public void RunProc(string procName, out SqlDataReader dataReader)
    {
      SqlCommand command = this.CreateCommand(procName, (SqlParameter[]) null);
      dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
    }

    public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
    {
      SqlCommand command = this.CreateCommand(procName, prams);
      dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
    }

    private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
    {
      this.Open();
      SqlCommand sqlCommand = new SqlCommand(procName, this.con);
      sqlCommand.CommandType = CommandType.StoredProcedure;
      if (prams != null)
      {
        foreach (SqlParameter pram in prams)
          sqlCommand.Parameters.Add(pram);
      }
      return sqlCommand;
    }

    public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
    {
      return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
    }

    public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
    {
      return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Output, (object) null);
    }

    public SqlParameter MakeParam(string ParamName, SqlDbType DbType, int Size, ParameterDirection Direction, object Value)
    {
      SqlParameter sqlParameter = Size <= 0 ? new SqlParameter(ParamName, DbType) : new SqlParameter(ParamName, DbType, Size);
      sqlParameter.Direction = Direction;
      if (Direction != ParameterDirection.Output || Value != null)
        sqlParameter.Value = Value;
      return sqlParameter;
    }

    private void Open()
    {
      if (this.con != null)
        return;
      this.con = new SqlConnection(SQLHelper.ConnectString);
      this.con.Open();
    }

    public void Close()
    {
      if (this.con == null)
        return;
      this.con.Close();
    }

    public void Dispose()
    {
      if (this.con == null)
        return;
      this.con.Dispose();
      this.con = (SqlConnection) null;
    }
  }
}
